﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Net;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Diagnostics;
using EncriptionLibrary;

namespace Goa
{
    public partial class Form1 : Form
    {

        [STAThread]
        static void Main(String[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        List<Patient> patienten = new List<Patient>();
        Dictionary<int, List<Session>> patientenDic = new Dictionary<int, List<Session>>();
        List<Session> sessions = new List<Session>();
        private int currentPatient = 0, selectedPatient;
        private int currentSessionCounter = 0;

        private MultiServer server;
        private int heartrate;
        private int rpm;
        private double speed;
        private double distance;
        private string time;
        private int energy;
        private int pulse;
        private int power;
        private int timerCount = 0;
        private Database a;
        private BewerkPatient wijzigPatient;
        private Boolean started = false;


        public Form1()
        {

            
            a = new Database();
            //Initializes the GUI
            InitializeComponent();
           
        }

        public void krijgTerugPatient(string s,string p,string w) { }

        public void RemoveOtherCharacters(string str)
        {
            string c = "";
            int select = 0;
            if ((str[0] >= '0' && str[0] <= '9')){
                for (int i = 0; i < str.Length; i++)
                {
                    if ((str[i] >= '0' && str[i] <= '9') || (str[i] == ':'))
                    {
                        c += str[i];
                    }
                    else
                    {
                        try
                        {
                            switch (select)
                            {
                                case 0:
                                    heartrate = Convert.ToInt32(c);
                                    break;
                                case 1:
                                    rpm = Convert.ToInt32(c);
                                    break;
                                case 2:
                                    speed = Convert.ToDouble(c) / 10;
                                    break;
                                case 3:
                                    distance = Convert.ToDouble(c) / 10;
                                    break;
                                case 4:
                                    power = Convert.ToInt32(c);
                                    break;
                                case 5:
                                    energy = Convert.ToInt32(c);
                                    break;
                                case 6:
                                    time = c;
                                    break;
                                case 7:
                                    pulse = Convert.ToInt32(c);
                                    break;
                            }
                        }
                        catch(FormatException)
                        {

                        }
                        c = "";
                        select++;
                    }
                }
            }
        }

        public void updateMY(int id, string message) {
            RemoveOtherCharacters(message);

            if (!patientenDic.ContainsKey(id))
            {
                patientenDic.Add(id, new List<Session>());
                patientenDic[id].Add(new Session(0, id.ToString()));
                patientenDic[id][0].addMeasurements(new MeasuredValues(heartrate, rpm, speed, distance, power, energy, time));
            }
            else
            {
                patientenDic[id][0].addMeasurements(new MeasuredValues(heartrate, rpm, speed, distance, power, energy, time));
            }
        }

        public void disconnectedClient(int id) {
            Console.WriteLine("Client {0} disconnected and will be removed", id);
            //Write to database!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            int current = Convert.ToInt32(a.getHighestSessionID(id.ToString())) + 1;
            int i = 0;
            try
            {
                foreach (MeasuredValues mv in patientenDic[id][0].ListOfValues)
                {
                    a.writeSession(id.ToString(), current.ToString(), mv.Power.ToString(), "timestamp" + i, mv.Rpm.ToString(), mv.Speed.ToString().Replace(",", "."),
                        mv.Distance.ToString().Replace(",", "."), mv.Time, mv.Energie.ToString(), mv.Heartrate.ToString());
                    i++;
                }
            }
            catch (KeyNotFoundException) { Console.WriteLine("No key for this one"); }
            patientenDic.Remove(id);
            //clients.Remove(client);
        }
       
        
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void patientgegevensToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            wijzigPatient = new BewerkPatient(this, a);
            wijzigPatient.ShowDialog();
        }

        private void Btn_StartServer_Click(object sender, EventArgs e)
        {
            server = new MultiServer(this);
        }
    }
}
